<?php
/**
 * Class general all function for user
 * @author NguyenVanTien
 *
 */
class Webservices_Users {

    /**
     * @var Application_Model_Users $_modelUsers
     */
    private $_modelUsers = null;

    public function __construct() {
        $this->_modelUsers = new Application_Model_Users();
    }
    
    /**
     * Add user
     * @param string $data
     * @return string
     */
    public function addUser($data){
        $res = array();
        $data = json_decode($data);
        $data->country_id = $data->countryId;
        $data = Webservices_Array_Format::convertObjectToArray($data, $res);
        $res = $this->_modelUsers->addUser($res);
        return $res;
    }

    /**
     * Login by user name and password
     * @param string $username
     * @param string $password
     * @return boolean
     */
    public function login($username, $password) {
        return $this->_modelUsers->login($username, $password);
    }

    /**
     * get user name and password
     * @param string $password
     * @param string $username
     * @return string
     */
    public function getUser($username, $password) {
        $rs = array();
        if ($this->login($username, $password)) {
            $user = $this->_modelUsers->fetchRow('username ="' . $username . '"');
            if ($user) {
                $rs = $user->toArray();
                $rs['countryId'] = $rs['country_id'];
                $rs['userId'] = $rs['user_id'];
            }
        }
        return json_encode($rs);
        
        
    }
    
    /**
     * Get user by user name
     * @author tien nguyen
     * @param string $username
     * @return string
     */
    public function getUserByUsername($username){
        $res = array();
        $user = $this->_modelUsers->getUserByUsername($username);
        if($user){
            $res = $user->toArray();
            $this->_modelUsers->convertField($res);
        }
        return json_encode($res);
    }

    
    /**
     * Change password
     * @param string $username
     * @param string $password
     * @param string $newPassword 
     * @return boolean
     */
    public function changePassword($username, $password, $newPassword){
        $res = false;
        if($this->login($username, $password)){
            $res = $this->_modelUsers->changePassword($username, $newPassword);
        }
        return $res;
    }
    
    /**
     * Update user 
     * @param string $username
     * @param string $password
     * @param string $data
     * @return boolean
     */
    public function updateUser($username, $password, $data){
        
        $data = json_decode($data);
        
        $data = $this->_modelUsers->convertObjectToArray($data);
        $this->_modelUsers->convertField2($data);
        
        return $this->_modelUsers->edit($data);
    }
    
    /**
     * change profile imformation
     * @author Tien Nguyen
     * @param string $username
     * @param string $password
     * @param string $data 
     * @return boolean
     */
    public function changeProfile($username, $password, $data){
        $xml = simplexml_load_string($data);
        $res = $this->_modelUsers->changeProfile($username, $password, $xml);
        return $res;
    }
    
    /**
     * Get list user friends
     * @author Tien Nguyen
     * @param string $username
     * @param string $password
     * @return string 
     */
    public function getFriends($username, $password){
        $res = '';
        if($this->_modelUsers->login($username, $password)){
            $userFriends = $this->_modelUsers->getFriends($username);
            if($userFriends){
                $res = $userFriends->toArray();
                foreach($userFriends as $k => $item){
                    $this->_modelUsers->convertField($res[$k]);
                }
            }
        }
        return json_encode($res);
    }
    
    /**
     * Add friends
     * @author Tien Nguyen
     * @param string $username
     * @param string $password
     * @param int $friendId 
     * @return int
     */
    public function addFriend($username, $password, $friendId){
        $rs = 0;
        if($this->_modelUsers->login($username, $password)){
            $user = $this->_modelUsers->getByUsername($username);
            if($user){
                $modelUserFriend = new Application_Model_DbTable_UserFriends();
                $rs = $modelUserFriend->addFriend($user->user_id, $friendId);
            }
        }
        
        return $rs;
    }
    
    /**
     * removw friend
     * @param string $username
     * @param string $password
     * @param int $friendId 
     * @author Tien Nguyen
     * @return boolean
     */
    public function removeFriend($username, $password, $friendId){
        $rs = false;
        if($this->_modelUsers->login($username, $password)){
            $user = $this->_modelUsers->getByUsername($username);            
            if($user){
                $modelUserFriend = new Application_Model_DbTable_UserFriends();
                $rs = $modelUserFriend->removeFriend($user->user_id, $friendId);
            }
        }
        return $rs;
    }
    
    /**
     * find fiends
     * @param string $username
     * @param string @passsword
     * @param string $name
     * @return string
     */
    public function findUser($username, $password, $name){
        $res = "";
        if($this->_modelUsers->login($username, $password)){
            $users = $this->_modelUsers->findUser($name);
            $res = json_encode($users);
        }
        return $res;
    }

    /**
     * View xml
     * @param string $xml
     * @return string
     */
    public function viewxml($xml) {
        return '<?xml version="1.0"?><data>' . $xml . '</data>';
    }
}